﻿using System;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Windows.Media;
using System.Drawing.Imaging;
using System.IO;

namespace 卷书
{
    internal class BitmapHelper
    {

        public static BitmapImage BitmapToBitmapImage(Bitmap bitmap, bool isPng = false)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                if (isPng)
                    bitmap.Save(stream, ImageFormat.Png);
                else
                    bitmap.Save(stream, ImageFormat.Jpeg);
                stream.Position = 0;
                BitmapImage result = new BitmapImage();
                result.BeginInit();
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
                result.Freeze();

                return result;
            }
        }
    }
}
